package nk;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.sparql.core.TriplePath;
import com.hp.hpl.jena.sparql.syntax.ElementGroup;
import com.hp.hpl.jena.sparql.syntax.ElementOptional;
import com.hp.hpl.jena.sparql.syntax.ElementPathBlock;
import com.hp.hpl.jena.sparql.syntax.ElementUnion;
import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase;
import com.hp.hpl.jena.sparql.syntax.ElementWalker;

public class SparqlToGraph {

	// static List<String> lista = new ArrayList<String>();
	static Map<Integer, Object> lista = new HashMap<Integer, Object>();
	static int cont = 0;
	static Arvore arvore = new Arvore();

	public void adiciona(Object obj) {
		cont = cont + 1;
		lista.put(cont, obj);
	}

	public static void main(String[] args) {

		final SparqlToGraph sparql = new SparqlToGraph();

		String queryString = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
				+ " PREFIX vCard: <http://www.w3.org/2001/vcard-rdf/3.0#>"
				+ " SELECT ?name"
				+ " WHERE"
				+ " {"
				+ " { [] foaf:name ?name1}"
				+ " UNION"
				+ " { [] vCard:FN ?name2}"
				+ " OPTIONAL { ?c  foaf:given   ?given ; foaf:family  ?family } "
				+ " }";

		// String queryString
		// =" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
		// +" PREFIX s: <http://teste#>"
		// +" SELECT ?org1"
		// +" FROM <Semiport.owl>"
		// +" WHERE"
		// +" {"
		// +" {"
		// +" {?org1 rdf:type s:Organization}"
		// +" UNION"
		// +" {?org1 rdf:type s:Department}"
		// +" UNION"
		// +" {?org1 rdf:type s:ResearchGroup}"
		// +" UNION"
		// +" {?org1 rdf:type s:University}"
		// +" }"
		// +" OPTIONAL"
		// +" {?orgSub rdf:type s:Departament}}";

		// "PREFIX vcard:      <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?givenName WHERE {{ ?y vcard:Family \"Smith\".} union {OPTIONAL {?y vcard:Given  ?givenName . }}}";
		// String queryString =
		// "prefix : <http://www.epimorphics.com/tools/example#>"
		// + " \nconstruct {"
		// + " \n:a :P ?v1. ?v1 :Q ?v2."
		// + " \n:a :P ?v3. ?v3 :R ?v4."
		// + " \n}"
		// + " \nwhere{"
		// + " \n{:a :P ?teste1 OPTIONAL {?v1 :Q ?teste2}}"
		// + " \nUNION {:a :P ?teste3 OPTIONAL {?v3 :R ?teste4}}" + "\n}";
		Query query = QueryFactory.create(queryString, Syntax.syntaxSPARQL_11);
		ElementGroup elp = (ElementGroup) query.getQueryPattern();

		final Set<Node> subjects = new HashSet<Node>();

		// This will walk through all parts of the query
		ElementWalker.walk(query.getQueryPattern(),
		// For each element...
				new ElementVisitorBase() {
					// ...when it's a block of triples...

					public void visit(ElementPathBlock el) {
						System.out.println("-ElementPathBlock");
						arvore.cria("ElementPathBlock");
						ElementPathBlock elCopy = new ElementPathBlock();
						Iterator<TriplePath> triples = el.patternElts();

						while (triples.hasNext()) {
							TriplePath t = triples.next();
							System.out.println(t.getObject());
							sparql.adiciona(t.getObject());
						}
						el = elCopy;
					}

					public void visit(ElementOptional el) {
						System.out.println("-ElementOptional");
						sparql.adiciona("OPTIONAL");
					}

					public void visit(ElementUnion el) {
						System.out.println("-ElementUnion");
						sparql.adiciona("UNION");
					}
				});
		// for (int i = 0; i < lista.size(); i++) {
		// if(!lista..containsValue("ElementOptional") &&
		// !lista.get(i).contains("ElementUnion")) {
		// sparql.adiciona("AND");
		// }
		// }
		// for (int i = 0; i < lista.size(); i++) {
		System.out.println(lista);
		// }

	}
}
